#!/bin/bash
lines=86


echo "解压安装包"
if [ -d "./envs" ]
then
rm -fr envs
fi
tail -n +$lines "$0" > envs.tar.gz
#xz -d nodes.tar.xz
tar -xvf envs.tar.gz


if [ ! -d /opt/ros/foxy/ ]
then
if [ -f ./envs/foxy.tar.gz ]
then
echo "安装ros"
#sudo apt-get install -y ros-foxy-desktop
sudo tar -xzvf ./envs/foxy.tar.gz -C /
fi
fi

#libs
echo "拷贝libs"
if [ ! -d ~/ros2_ws/nodelibs/ ]
then
mkdir -p ~/ros2_ws/nodelibs/
fi
 
cp -fr ./envs/nodelibs/* ~/ros2_ws/nodelibs/


if [ -d ./envs/run_model ] 
then
#安装yolo依赖
cd ./envs
#获取q10驱动文件
cp -fr ./cap_4328 -d ~/
#copy run_model
cp -fr ./run_model  ~/

#pymavlink
sudo pip install ./pymavlink-2.4.41-py3-none-any.whl
#ultralytics
sudo pip install ./ultralytics-8.1.30-py3-none-any.whl
#filterpy
unzip filterpy-1.4.5.zip
mv ./filterpy-1.4.5 ~/ros2_ws/nodelibs/
sudo python3 ~/ros2_ws/nodelibs/filterpy-1.4.5/setup.py install
cd ..
fi

#install includes 拷贝依赖库头文件
sudo cp -fr ./envs/inc/* /usr/local/include/


sudo rm -fr envs
rm envs.tar.gz

#安装q10驱动
cd ~/cap_4328/
sudo ./install.sh

addSource(){
src=$(grep -i "${1}" ~/.bashrc)
len=$(expr length "$src")
if [ $len -eq 0 ] 
then
echo "source ${1}" | tee -a ~/.bashrc
fi
}



#增加source
addSource "/opt/ros/foxy/setup.bash"
addSource "~/ros2_ws/install/setup.bash"

#增加
echo "export LD_LIBRARY_PATH=~/ros2_ws/nodelibs:$LD_LIBRARY_PATH"
echo "安装完成"

exit 0
